package com.yxt.dispatch.service.impl;

import com.yxt.common.utils.ajax.PageUtils;
import com.yxt.common.utils.ajax.Query;
import com.yxt.common.utils.ajax.R;
import com.yxt.dispatch.entity.FormEntity;
import com.yxt.dispatch.entity.FormItemEntity;
import com.yxt.dispatch.mapper.FormItemMapper;
import com.yxt.dispatch.mapper.FormMapper;
import com.yxt.dispatch.service.FormItemService;
import com.yxt.dispatch.service.FormService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("formService")
public class FormServiceImpl extends ServiceImpl<FormMapper, FormEntity> implements FormService {

    @Autowired
    FormItemService formItemService;

    @Autowired
    FormItemMapper formItemMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FormEntity> page = this.page(
                new Query<FormEntity>().getPage(params),
                new QueryWrapper<FormEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R copy(String source, String destination, String formName) {
        //复制选中的表单
        if(StringUtils.isNotBlank(formName)){
            FormEntity sourceItme = this.getOne(new QueryWrapper<FormEntity>()
                    .eq("area_id", source)
                    .eq("form_name",formName));

            List<FormItemEntity> sourceItmes = formItemService.list(new QueryWrapper<FormItemEntity>()
                    .eq("form_id", sourceItme.getFormId()));

            //删除
            FormEntity destItme = this.getOne(new QueryWrapper<FormEntity>()
                    .eq("area_id", destination)
                    .eq("form_name",formName));

            if(destItme!=null){
                //删除formItems
                List<FormItemEntity> destItmes = formItemService.list(new QueryWrapper<FormItemEntity>()
                        .eq("form_id", destItme.getFormId()));
                List<Long> itemsIds = destItmes.stream().map(FormItemEntity::getFormItemId).collect(Collectors.toList());
                formItemService.removeByIds(itemsIds);
                //删除formItem
                this.removeById(destItme.getFormId());
            }

            //插入新的formitem
            FormEntity formEntity = new FormEntity();
            formEntity.setAreaId(destination);
            formEntity.setFormName(formName);
            this.save(formEntity);

            //插入新的formItems
            List<FormItemEntity> formItems = sourceItmes.stream().map(attr -> {
                FormItemEntity formItemEntity = new FormItemEntity();
                BeanUtils.copyProperties(attr,formItemEntity);
                formItemEntity.setFormId(formEntity.getFormId());
                formItemEntity.setFormItemId(null);
                return formItemEntity;
            }).collect(Collectors.toList());
            formItemService.saveBatch(formItems);
        }else{
            //复制此区域所有的表单
            List<FormEntity> sourcesItem = this.list(new QueryWrapper<FormEntity>()
                    .eq("area_id", source));
            List<FormEntity> destsItem = this.list(new QueryWrapper<FormEntity>()
                    .eq("area_id", destination));

            //删除
            if(destsItem!=null && destsItem.size()>0){
                for(FormEntity formEntity : destsItem){
                    formItemService.remove(new QueryWrapper<FormItemEntity>()
                            .eq("form_id", formEntity.getFormId()));
                    this.removeById(formEntity.getFormId());
                }
            }

            //新增
            //插入新的formItems
            for(FormEntity formEntity : sourcesItem){
                FormEntity entity = new FormEntity();
                entity.setAreaId(destination);
                entity.setFormName(formEntity.getFormName());
                this.save(entity);

                //查询出所有的表单项
                List<FormItemEntity> sourceFormItems = formItemService.list(new QueryWrapper<FormItemEntity>()
                        .eq("form_id", formEntity.getFormId()));

                List<FormItemEntity> destFormItems = sourceFormItems.stream().map(item -> {
                    FormItemEntity formItemEntity = new FormItemEntity();
                    BeanUtils.copyProperties(item, formItemEntity);
                    formItemEntity.setFormId(entity.getFormId());
                    formItemEntity.setFormItemId(null);
                    return formItemEntity;
                }).collect(Collectors.toList());

                formItemService.saveBatch(destFormItems);
            }
        }


        return R.ok();
    }

}